package me.shurufa.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import me.shurufa.utils.LogUtils;

public class MaskSurfaceView extends FrameLayout{

	private MaskView imageView;
	private int width;
	private int height;
	private int maskWidth;
	private int maskHeight;
	private int screenWidth;
	private int screenHeight;
	
	public MaskSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		imageView = new MaskView(context);
		this.addView(imageView,LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		
		Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		screenHeight = display.getHeight();
		screenWidth = display.getWidth();
	}
	
	public void setMaskSize(Integer width, Integer height){
		maskHeight = height;
		maskWidth = width;
	}
	
	public int[] getMaskSize(){
		return new MaskSize().size;
	}

	private class MaskSize{
		private int[] size;
		private MaskSize(){
			this.size = new int[]{maskWidth, maskHeight, screenWidth, screenHeight};
		}
	}
	
	private class MaskView extends View{
		private Paint linePaint;
		private Paint rectPaint;
		public MaskView(Context context) {
			super(context);
			
//			绘制中间透明区域矩形边界的Paint
			linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			linePaint.setColor(Color.WHITE);
			linePaint.setStyle(Style.STROKE);
			linePaint.setStrokeWidth(6f);
			linePaint.setAlpha(255);

			//绘制四周矩形阴影区域
			rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			rectPaint.setStyle(Style.FILL);
			rectPaint.setAlpha(100);
		}
		@Override
		protected void onDraw(Canvas canvas) {
			if(maskHeight==0 && maskWidth==0){
				return;
			}
			if(maskHeight==screenHeight || maskWidth==screenWidth){
				return;
			}
			
//			if((height>width&&maskHeight<maskWidth) || (height<width&&maskHeight>maskWidth)){
//				int temp = maskHeight;
//				maskHeight = maskWidth;
//				maskWidth = temp;
//			}
			
			int h = Math.abs((screenHeight-maskHeight)/2);
			int w = Math.abs((screenWidth-maskWidth)/2);
			
////			上
//			canvas.drawRect(0, 0, width, h, this.linePaint);
////			右
//			canvas.drawRect(width-w, h, width, height-h, this.linePaint);
////			下
//			canvas.drawRect(0, height-h, width, height, this.linePaint);
////			左
//			canvas.drawRect(0, h, w, h+maskHeight, this.linePaint);
			LogUtils.kLog().e("w"+w+"h"+h+",w+maskwidth"+(w+maskWidth)+"h+maskHeight"+(h+maskHeight));
			canvas.drawRect(w, h, w+maskWidth, h+maskHeight, this.linePaint);
			
			super.onDraw(canvas);
		}
	}
}
